package labox.innovation.gameserver.network.clientpackets;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import labox.innovation.config.Config;
import labox.innovation.gameserver.model.L2ItemInstance;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;
import labox.innovation.gameserver.network.SystemMessageId;
import labox.innovation.gameserver.network.serverpackets.InventoryUpdate;
import labox.innovation.gameserver.network.serverpackets.SystemMessage;

/**
 *  卸载装备
 * @author zjj
 *
 */
public class RequestUnEquipItem extends L2GameClientPacket {
	private static final String _C__78_REQUESTUNEQUIPITEM = "[C] 0x78 RequestUnequipItem";

	private static final Logger _log = LoggerFactory.getLogger(RequestUnEquipItem.class.getName());

	// cd
	//装备的 插槽位置
	private int _slot;

	/**
	 * packet type id 0x11
	 * format:		cd
	 * @param decrypt
	 */
	@Override
	protected void readImpl() {
		_slot = readByte();
		if (Config.DEBUG)
			_log.debug( "卸载的插槽位置:" + _slot) ;
	}

	@Override
	protected void runImpl() {
		if (_log.isDebugEnabled())
			_log.debug("request unequip slot {}", _slot);

		FuzePcInstance activeChar = getClient().getActiveChar();

		if (activeChar == null)
			return;

//		L2ItemInstance item = activeChar.getInventory().getPaperdollItemByL2ItemId(_slot);
		L2ItemInstance item = activeChar.getInventory().getPaperdollItem(_slot);
		if (item != null && item.isWear()) {
			// Wear-items are not to be unequipped
			return;
		}

		// Prevent player from unequipping items in special conditions
		if (activeChar.isStunned() || activeChar.isSleeping() || activeChar.isParalyzed()) {
			activeChar.sendMessage("Your status does not allow you to do that.");
			return;
		}
		//        if (activeChar.isCastingNow() || activeChar.isCastingSimultaneouslyNow())
		//        	return;

		L2ItemInstance[] unequiped =
		//			activeChar.getInventory().unEquipItemInBodySlotAndRecord(_slot);
		activeChar.getInventory().unEquipItemInSlotAndRecord(_slot);

		// show the update in the inventory
		InventoryUpdate iu = new InventoryUpdate();

		for (L2ItemInstance itm : unequiped) {
			iu.addModifiedItem(itm);
		}

		activeChar.sendPacket(iu);

		// this can be 0 if the user pressed the right mousebutton twice very fast
		if (unequiped.length > 0) {

			SystemMessage sm = null;

			sm = new SystemMessage(SystemMessageId.S1_DISARMED);
			sm.addItemName(unequiped[0]);

			activeChar.sendPacket(sm);
			sm = null;
		}

	}

	/* (non-Javadoc)
	 * @see labox.innovation.gameserver.clientpackets.ClientBasePacket#getType()
	 */
	@Override
	public String getType() {
		return _C__78_REQUESTUNEQUIPITEM;
	}
}
